import React from 'react'
import Address from '../../components/Address'
import { withRouter } from 'react-router-dom'
import Tit from '../../components/Tit'
class FillOrder extends React.Component {
    state = {
        ByGood: null,
        Address: null,
        yhPay: 0,
        xxx: true,
        statusCode: null,
        totalPrice: 0
    }
    goOK = () => {
        console.log(this.state.statusCode == 1)
        if (this.props.match.params.ID == 0 && this.state.xxx) {
            this.setState({
                xxx: false
            })
            let a = { commonId: this.props.match.params.commonId, goodsId: this.props.match.params.goodsId, num: this.props.match.params.num, addressId: this.props.match.params.addressId, num: this.props.match.params.num, inviteCode: this.props.match.params.inviteCode }
            this.$postData('/api/order/buyNow', a, params => {
                console.log(params)
                switch (params.code) {
                    case 100:
                        if (this.state.statusCode == 1) {
                            this.$postData('/api/payApi/phonePayOrder', { orderNo: params.data.orderNumber, payType: 2, type: 1, platformType: 3 }, (params) => {
                                console.log('FLSH')
                            })
                        } else {
                            this.props.history.push(`/PayClass/${params.data.orderNumber}`)
                        }
                        break;
                    default:
                        this.Toast.info(params.msg);
                        this.setState({
                            xxx: true
                        })
                        break;
                }
            })
        }
        else if (this.props.match.params.ID == 1 && this.state.xxx) {
            this.setState({
                xxx: false
            })
            let a
            this.props.match.params.inviteCode === "''" ? a = { cartId: this.props.match.params.commonId, inviteCode: 0, addressId: this.props.match.params.addressId }
                : a = { cartId: this.props.match.params.commonId, inviteCode: this.props.match.params.inviteCode, addressId: this.props.match.params.addressId }
            this.$postData('/api/order/cartOrder', a, params => {
                console.log(params)
                switch (params.code) {
                    case 100:
                        if (this.state.statusCode == 1) {
                            this.$postData('/api/payApi/phonePayOrder', { orderNo: params.data.orderNumber, payType: 2, type: 1, platformType: 3 }, (params) => {
                                console.log('FLSH')
                            })
                        } else {
                            this.props.history.push(`/PayClass/${params.data.orderNumber}`)
                        }
                        break;
                    default:
                        this.Toast.info(params.msg);
                        this.setState({
                            xxx: true
                        })
                        break;
                }
            })
        }
        // this.props.history.push('/payok')
    }
    AddCard() {
        this.props.history.push(`/AddCard/${this.props.match.params.ID}/${this.props.match.params.commonId}/${this.props.match.params.goodsId}/${this.props.match.params.num}/${this.props.match.params.addressId}/${this.props.match.params.inviteCode}`)
    }
    AllAddress = () => {
        this.props.history.push(`/AllAddress/${this.props.match.params.ID}/${this.props.match.params.commonId}/${this.props.match.params.goodsId}/${this.props.match.params.num}/${this.props.match.params.addressId}/${this.props.match.params.inviteCode}`)
    }
    componentDidMount() {
        console.log(this.props.match.params)
        this.$getData('/api/otherInfo/isPhone', params => {
            console.log(params)
            switch (params.code) {
                case 100:
                    this.setState({
                        statusCode: params.statusCode
                    })
                    break;
                default:
                    this.Toast.info(params.msg);
                    break;
            }
        })
        if (this.props.match.params.ID == 0) {
            this.$postData('/api/order/toPageByGood', { commonId: this.props.match.params.commonId, goodsId: this.props.match.params.goodsId, num: this.props.match.params.num }, params => {
                console.log(params)
                switch (params.code) {
                    case 100:
                        this.setState({
                            ByGood: [params.data]
                        })
                        break;
                    default:
                        this.Toast.info(params.msg);
                        break;
                }
            })
        } else {
            console.log('aaa')
            this.$postData('/api/order/toPageByCart', { cartId: this.props.match.params.commonId }, params => {
                console.log(params)
                switch (params.code) {
                    case 100:
                        this.setState({
                            ByGood: params.data.voList,
                            totalPrice: params.data.totalPrice
                        })
                        break;
                    default:
                        this.Toast.info(params.msg);
                        break;
                }

            })
        }
        if (this.props.match.params.addressId === `''`) {
            this.$postData('/api/address/getDefaultAddress', {}, params => {
                console.log(params)
                if (params.data.areaId === '') {
                    this.Toast.info('请先选择地址');
                } else {
                    this.props.match.params.addressId = params.data.id
                    this.setState({
                        Address: params.data
                    })
                }
            })
        } else {
            this.$postData('/api/address/getAddressById', { id: this.props.match.params.addressId }, params => {
                console.log(params)
                this.props.match.params.addressId = params.data.id
                this.setState({
                    Address: params.data
                })
            })
        } if (this.props.match.params.inviteCode === `''`) {
            return
        } else {
            this.props.match.params.ID==0?this.$postData('/api/order/validaCode', { inviteCode: this.props.match.params.inviteCode, type: this.props.match.params.ID, goodsId: this.props.match.params.goodsId, num: this.props.match.params.num }, params => {
                console.log(params)
                switch (params.code) {
                    case 100:
                        // this.Toast.info(params.msg);
                        this.setState({
                            yhPay: params.data
                        })
                        break;
                    default:
                        this.Toast.info(params.msg);
                        break;
                }
            }):this.$postData('/api/order/validaCode', { inviteCode: this.props.match.params.inviteCode, type: this.props.match.params.ID, cartId: this.props.match.params.commonId }, params => {
                console.log(params)
                switch (params.code) {
                    case 100:
                        // this.Toast.info(params.msg);
                        this.setState({
                            yhPay: params.data
                        })
                        break;
                    default:
                        this.Toast.info(params.msg);
                        break;
                }
            })
        }
    }
    render() {
        return (<div className='FillO-page'>
            {this.state.ByGood ? <div>
                <Tit zz={true} tit={'订单详情'}></Tit>
                {this.state.Address ? <div onClick={this.AllAddress}><Address ID={1} index={0} address={this.state.Address}></Address></div> : <div></div>}
                <div className='filO-d1'>
                    <p>商品金额</p>
                    <p>¥{this.props.match.params.ID == 0 ? this.state.ByGood[0].totalPrice : this.state.totalPrice}</p>
                </div>
                <div className='filO-d1'>
                    <p>运费</p>
                    <p>包邮</p>
                </div>
                <div className='filO-d2 xin' onClick={this.AddCard.bind(this)}>
                    <p>填写推广码</p>
                    {this.state.yhPay ? <p className='filO-d2-p3'>您已享受优惠{this.state.yhPay}元</p> : <p className='filO-d2-p2'>填写推广可享受优惠</p>}
                </div>
                {this.state.ByGood.map((val, index) => (
                    <div className='ShopD-d5-d2' key={index}>
                        <img className='ShopD-d5-d2-i1' src={require('../../static/img/guan.png')} alt="" />
                        <div className='ShopD-d5-d3'>
                            <div className='ShopD-d5-d3-d1'>
                                <p>{val.goodsCommonName}</p>
                                <p>x{val.num}</p>
                            </div>
                            <p className='ShopD-d5-d3-p1'>{val.specInfo}</p>
                            <p className='ShopD-d5-d3-p2'>¥{val.price}</p>
                        </div>
                    </div>
                ))}
                <div className='filO-d3'>
                    <p>应付：¥{this.props.match.params.ID == 0 ? this.state.ByGood[0].totalPrice : this.state.totalPrice - this.state.yhPay}</p>
                    <p className='center' onClick={this.goOK}>
                        去付款
                        </p>

                </div></div> : false} </div>
        )
    }
}

export default withRouter(FillOrder)